package com.liang.ui;

import cn.hutool.core.io.FileUtil;
import com.liang.utils.ExcelCopy;
import com.liang.utils.TimeUtils;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Map;

/**
 * @ClassName ExcelCopyUI
 * @Description TODO
 * 拷贝指定文件夹下所有的excel文件到另一个excel文件中
 * @Author liang
 * @Date 2019/7/4 14:50
 * @Version 1.0
 **/
public class ExcelCopyUI extends JFrame {
    //定义组件
    JPanel jPanel;
    JScrollPane jScrollPane;
    JTextField textField;
    JButton uploadBt;
    JButton copydBt;
    JFileChooser chooser;//文件上传
    String choosertitle;
    JTextArea textArea;

    ArrayList<String> fileList;//存放文件列表

    public static final String extensively = "友情提示\n"
            + "1. 将收集的excel放在同一目录下\n"
            + "2. 选择此目录,然后点合并\n"
            ;


    /**
     * 构造面板
     */
    public ExcelCopyUI(){
        jPanel = new JPanel();
        textArea = new JTextArea();
        jScrollPane = new JScrollPane(textArea);
        textField = new JTextField(15);
        uploadBt = new JButton("选择文件夹");
        copydBt = new JButton("合并");
        //说明
        textArea.setText(extensively);

        jPanel.add(textField);
        jPanel.add(uploadBt);
        jPanel.add(copydBt);

        //布局
        this.add(jScrollPane);
        this.add(jPanel, BorderLayout.SOUTH);
        this.setSize(800, 600);
        this.setTitle("批量获取当天邮件内容");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        //监听按钮
        action();

    }

    private void action() {
        //上传动作
        uploadBt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                chooser = new JFileChooser();
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                //默认打开当前文件所在的目录
                chooser.setCurrentDirectory(new File("d:/"));
                chooser.setDialogTitle(choosertitle);
                if(chooser.showOpenDialog(jPanel) == JFileChooser.APPROVE_OPTION){
                    //File currentDirectory = chooser.getCurrentDirectory();
                    File dirPath = chooser.getSelectedFile();
                    textField.setText(dirPath.getAbsolutePath());
                    textArea.setText("");
                    textArea.append("选择的文件路径为:"+dirPath+"\n");
                    //遍历文件
                    File file = FileUtil.file(dirPath);
                    if(file.isDirectory()){
                        //文件列表
                        fileList =  new ArrayList<String>();
                        File[] files = file.listFiles();
                        int count = 0;
                        for(File f:files){
                            if(f.isFile()){
                                String filePath = f.getAbsolutePath();
                                if(filePath.endsWith(".xls") || filePath.endsWith(".xlsx")){
                                    count ++;
                                    textArea.append("excel文件路径为:"+f.getAbsolutePath()+"\n");
                                    fileList.add(f.getAbsolutePath());
                                }
                             }
                        }
                        if(count == 0){
                            textArea.append(dirPath+"下无excel文件");
                        }
                    }
                }else {
                    textArea.append("没选择文件\n");
                }
            }
        });

        //复制动作
        //上传动作
        copydBt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(textField.getText().length() == 0){
                    textArea.setText("请选择文件夹");
                }else {


                try{
                    //1.复制到文件中,获取文件路径
                    String filePath = "d:/邮件采集合并/邮件采集合并_"+ TimeUtils.getToday() +".xlsx";
                    //遍历目录,是否有excel文件
                    //如果有excel文件,一个个执行,获取内容,存入map
                    //最后写入新文件中
                    ExcelCopy copy = new ExcelCopy();
                    ArrayList<Map<String, Object>> maps = copy.excelToMap(fileList, filePath);
                    textArea.append("写入的数据为:\n");
                    for(Map map:maps){
                        textArea.append(map.toString()+"\n");
                    }
                    textArea.append("写入成功: "+filePath);
                }catch (Exception msg){
                    textArea.append("文件合并失败,"+msg.getMessage());
                }
                }
             }
        });

    }

    //文件过滤

    class xlsFilter extends FileFilter {

        @Override
        public boolean accept(File f) {
            if (f.isDirectory())return true;
            return f.getName().toLowerCase().endsWith(".xls");
        }

        @Override
        public String getDescription() {
            return ".xls";
        }
    }

    class xlsxFilter extends FileFilter{

        @Override
        public boolean accept(File f) {
            if (f.isDirectory())return true;
            return f.getName().toLowerCase().endsWith(".xlsx");
        }

        @Override
        public String getDescription() {
            return ".xlsx";
        }
    }
}
